home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 353_01 / chap06.txt < prev    next >
Text File  |  1992-01-18  |  37KB  |  767 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.                                                         Chapter 6
  8.                                                MORE ENCAPSULATION
  9.  
  10.  
  11. The purpose of this chapter is to illustrate how to use some of the
  12. traditional aspects of C or C++ with classes and objects.  Pointers
  13. to an object as well as pointers within an object will be
  14. illustrated.  Arrays embedded within an object, and an array of
  15. objects will be illustrated.  Since objects are simply another C++
  16. data construct, all of these things are possible and can be used
  17. if needed.
  18.  
  19. In order to have a systematic study, we will use the program named
  20. BOXES1.CPP from the last chapter as a starting point and we will
  21. add a few new constructs to it for each example program.  You will
  22. recall that it was a very simple program with the class definition,
  23. the class implementation, and the main calling program all in one
  24. file.  This was selected as a starting point because we will
  25. eventually make changes to all parts of the program and it will be
  26. convenient to have it all in a single file for illustrative
  27. purposes.  It must be kept in mind however that the proper way to
  28. use these constructs is to separate them into the three files as
  29. was illustrated in BOX.H, BOX.CPP, and BOXES2.CPP in the last
  30. chapter.  This allows the implementor of box to supply the user
  31. with only the interface, namely  BOX.H.  Not giving him the
  32. implementation file named BOX.CPP, is practicing the technique of
  33. information hiding.
  34.  
  35. As we have said many times, it seems silly to break up such a small
  36. program into three separate files, and it is sort of silly.  The
  37. last chapter of this tutorial will illustrate a program large
  38. enough to require dividing the program up into many separate files.
  39.  
  40.  
  41. AN ARRAY OF OBJECTS
  42. _________________________________________________________________
  43.  
  44. Examine the file named OBJARRAY.CPP for our      ================
  45. first example of an array of objects.  This file   OBJARRAY.CPP
  46. is nearly identical to the file named BOX1.CPP   ================
  47. until we come to line 44 where an array of 4
  48. boxes are declared.
  49.  
  50. Recalling the operation of the constructor you will remember that
  51. each of the four box objects will be initialized to the values
  52. defined within the constructor since each box will go through the
  53. constructor as they are declared.  In order to declare an array of
  54. objects, a constructor for that object must not require any
  55. parameters.  (We have not yet illustrated a constructor with
  56. initializing parameters, but we will in the next program.)  This
  57.  
  58.                                                          Page 6-1
  59.  
  60.                                    Chapter 6 - More Encapsulation
  61.  
  62. is an efficiency consideration since it would probably be an error
  63. to initialize all elements of an array of objects to the same
  64. value.  We will see the results of executing the constructor when
  65. we compile and execute the file later.
  66.  
  67. Line 49 defines a for loop that begins with 1 instead of the normal
  68. starting index for an array leaving the first object, named
  69. group[0], to use the default values stored when the constructor was
  70. called.  You will observe that sending a message to one of the
  71. objects uses the same construct as is used for any object.  The
  72. name of the array followed by its index in square brackets is used
  73. to send a message to one of the objects in the array.  This is
  74. illustrated in line 50 and the operation of that code should be
  75. clear to you.  The other method is called in the output statement
  76. in lines 57 and 58 where the area of the four boxes in the group
  77. array are listed on the monitor.
  78.  
  79. Another fine point should be pointed out.  The integer variable
  80. named index is declared in line 49 and is still available for use
  81. in line 56 since we have not yet left the enclosing block which
  82. begins in line 43 and extends to line 65.
  83.  
  84.  
  85.  
  86. DECLARATION AND DEFINITION OF A VARIABLE
  87. _________________________________________________________________
  88.  
  89. An extra variable was included for illustration, the one named
  90. extra_data in line seven.  Since the keyword static is used to
  91. modify this variable in line 7, it is an external variable and only
  92. one copy of this variable will ever exist.  All seven objects of
  93. this class share a single copy of this variable which is global to
  94. the objects defined in line 44.
  95.  
  96. The variable is actually only declared here which says it will
  97. exist somewhere, but it is not defined.  A declaration says the
  98. variable will exist and gives it a name, but the definition
  99. actually defines a place to store it somewhere in the computers
  100. memory space.  By definition, a static variable can be declared in
  101. a class header but it cannot be defined there, so it is defined
  102. somewhere outside of the header, usually in the implementation
  103. file.  In this case it is defined in line 16 and can then be used
  104. throughout the class.
  105.  
  106. Line 23 of the constructor sets the single global variable to 1
  107. each time an object is declared.  Only one assignment is necessary
  108. so the other six are actually wasted code.  To illustrate that
  109. there is only one variable shared by all objects of this class,
  110. the method to read its value also increments it.  Each time it is
  111. read in lines 60 through 64, it is incremented and the result of
  112. the execution proves that there is only a single variable shared
  113. by all objects of this class.  You will also note that the method
  114. named get_extra() is defined within the class declaration so it
  115. will be assembled into the final program as inline code.
  116.  
  117.                                                          Page 6-2
  118.  
  119.                                    Chapter 6 - More Encapsulation
  120.  
  121.  
  122. Be sure you understand this program and especially the static
  123. variable, then compile and execute it to see if you get the same
  124. result as listed at the end of the program.
  125.  
  126.  
  127. A STRING WITHIN AN OBJECT
  128. _________________________________________________________________
  129.  
  130. Examine the program named OBJSTRNG.CPP for our   ================
  131. first example of an object with an embedded        OBJSTRNG.CPP
  132. string.  Actually, the object does not have an   ================
  133. embedded string, it has an embedded pointer, but
  134. the two work so closely together that we can
  135. study one and understand both.
  136.  
  137. You will notice that line 7 contains a pointer to a string named
  138. line_of_text.  The constructor contains an input parameter which
  139. is a pointer to a string which will be copied to the string named
  140. line_of_text within the constructor.  We could have defined the
  141. variable line_of_text as an actual array in the class, then used
  142. strcpy() to copy the string into the object and everything would
  143. have worked the same, but we will leave that as an exercise for you
  144. at the end of this chapter.  It should be pointed out that we are
  145. not limited to passing a single parameter to a constructor.  Any
  146. number of parameters can be passed, as will be illustrated later.
  147.  
  148. You will notice that when the three boxes are declared this time,
  149. we supply a string constant as an actual parameter with each
  150. declaration which is used by the constructor to assign the string
  151. pointer some data to point to.  When we call get_area() in lines
  152. 48 through 53, we get the message displayed and the area returned.
  153. It would be prudent to put these operations in separate methods
  154. since there is no apparent connection between printing the message
  155. and calculating the area, but it was written this way to illustrate
  156. that it can be done.  What this really says is that it is possible
  157. to have a method that has a side effect, the message output to the
  158. monitor, and a return value, the area of the box.  However, as we
  159. discussed in chapter 4 when we studied DEFAULT.CPP, the order of
  160. evaluation is sort of funny, so we broke each line into two lines.
  161.  
  162. After you understand this program, compile and execute it.
  163.  
  164.  
  165. AN OBJECT WITH AN INTERNAL POINTER
  166. _________________________________________________________________
  167.  
  168. The program named OBJINTPT.CPP is our first      ================
  169. example program with an embedded pointer which     OBJINTPT.CPP
  170. will be used for dynamic allo